import Vue from 'vue'
import VueRouter from 'vue-router'
import Home from '../layout/Home.vue'
import Dash from '../views/Dashboard/index'
import local from '../utils/local.js'

// 获取原型对象上的push函数
const originalPush = VueRouter.prototype.push
VueRouter.prototype.push = function push(location) {
  return originalPush.call(this, location).catch(err => err)
}

Vue.use(VueRouter)

const routes = [
  {
    path: '/',
    redirect: '/home'
  },
  {
    path: '/home',
    component: Home,
    redirect: '/dashboard',
    children: [
      // 后台首页
      {
        path: '/dashboard',
        meta: { name: '后台首页' },
        component: Dash
      },
      // 订单管理
      {
        path: '/order',
        meta: { name: '订单管理' },
        component: () => import('../views/Order/index')
      },
      // 商品管理
      {
        path: '/goods',
        component: () => import('../views/Goods/index'),
        meta: { name: '商品管理' },
        redirect: '/goods/add',
        children: [
          // 商品添加
          {
            path: '/goods/add',
            meta: { name: '商品添加' },
            component: () => import('../views/Goods/add')
          },
          // 商品分类
          {
            path: '/goods/cate',
            meta: { name: '商品分类' },
            component: () => import('../views/Goods/cate')
          },
          {
            path: '/goods/list',
            meta: { name: '商品列表' },
            component: () => import('../views/Goods/list')
          }
        ]
      },
      // 账号管理
      {
        path: '/account',
        meta: { name: '账号管理' },
        component: () => import('../views/Account/index'),
        redirect: '/account/add',
        children: [
          // 添加账号
          {
            path: '/account/add',
            meta: { name: '添加账号' },
            component: () => import('../views/Account/add')
          },
          // 修改密码
          {
            path: '/account/change',
            meta: { name: '修改密码' },
            component: () => import('../views/Account/change')
          },
          // 账号列表
          {
            path: '/account/list',
            meta: { name: '账号列表' },
            component: () => import('../views/Account/list')
          },
          // 个人中心
          {
            path: '/account/person',
            meta: { name: '个人中心' },
            component: () => import('../views/Account/Personal')
          }
        ]
      },
      // 销售统计
      {
        path: '/total',
        component: () => import('../views/Total/index'),
        children: [
          // 订单统计
          {
            path: '/total/ordertotal',
            meta: { name: '订单统计' },
            component: () => import('../views/Total/ordertotal')
          },
          // 商品统计
          {
            path: '/total/shoptotal',
            meta: { name: '商品统计' },
            component: () => import('../views/Total/shoptotal')
          }
        ]
      },
      // 店铺管理
      {
        path: '/shop',
        meta: { name: '店铺管理' },
        component: () => import('../views/Shop/index')
      }
    ]
  },
  // 登录页
  {
    path: '/login',
    component: () => import('../views/Login.vue')
  }
]

// component: () => import(/* webpackChunkName: "about" */ '../views/About.vue')

const router = new VueRouter({
  routes
})

// 导航守卫
router.beforeEach((to, from, next) => {
  // 导航守卫，目的是不允许未登录的用户进入路由页面
  const token = local.get('token')
  if (token) {
    next()
  } else {
    if (to.path === '/login') {
      next()
    } else {
      next('/login')
    }
  }
})

export default router
